angular.module('mamApp').controller('programAuditController', ['$scope', '$state', '$uibModal', 'ImsDialog', 'ProgramAuditManager', '$filter', 'AlbumManager', 'CommonManager', 'ImsAlert','$rootScope',
    function ($scope, $state, $uibModal, ImsDialog, ProgramAuditManager, $filter, AlbumManager, CommonManager, ImsAlert,$rootScope) {
        $scope.channels = [{channelId: "", channelName: "二级频道"}];

        $scope.albumId = $state.params.albumId;
        clearInterval($rootScope.timerProgress);





        $scope.fileNames = [{key: "", value: "查找字段"}, {key: "programId", value: "节目ID"}, {
            key: "programName",
            value: "节目名称"
        }, {key: "albumName", value: "专辑名称"}];
        $scope.search = {
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10,
            reviewStatus: $state.params.reviewStatus ? parseInt($state.params.reviewStatus) : "",
            adminId: $state.params.adminId ? parseInt($state.params.adminId) : "",
            channelId: $state.params.channelId ? parseInt($state.params.channelId) : "",
            key: $state.params.key || "",
            value: $state.params.value || ""
        };
        $scope.checkStatus = [{key: "", value: "审核状态"}, {key: 7, value: $scope.table[7]}, {
            key: 8,
            value: $scope.table[8]
        }, {
            key: 9,
            value: $scope.table[9]
        }];
        $scope.findData = function () {
            var resetSearch = {
                pageNo:1,
                reviewStatus: "",
                adminId: "",
                channelId: "",
                key: $scope.search.key,
                value: $scope.search.value

            }
            $state.go($state.current.name, resetSearch);
        }






        function programsIsChange() {
            var isChange = false;
            for (var i = 0;i < $scope.programList.length;i++) {
                if ($scope.programList[i].programId != $scope.programsCopy[i].programId) {
                    isChange = true;
                }
            }
            return isChange;


        }

        CommonManager.channelAllList().then(function (result) {
            $scope.search.pageNo=1;
            $scope.channels = $scope.channels.concat(result.data.pageList);
        })

        $scope.channelChange = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search);
        }
        $scope.reviewStatusChange = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search);
        }
        $scope.adminChange = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search);
        }


        $scope.keyword = $scope.search.keyword;

        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        };

        $scope.searchClick = function (keyword) {
            $scope.search.pageNo=1;
            $scope.search.keyword = keyword;
            $scope.search.reviewStatus = $scope.search.reviewStatus == 'null' ? null : $scope.search.reviewStatus;
            $state.go($state.current.name, $scope.search)

        };

        $scope.statusClick = function (reviewStatus) {
            $scope.search.pageNo=1;
            $scope.search.keyword = null;

            $scope.search.reviewStatus = status == 'null' ? null : reviewStatus;
            $state.go($state.current.name, $scope.search)

        };
        $scope.noPermission = false;
        //专辑ID有的话取专辑下面的节目
        if ($scope.albumId) {

            AlbumManager.albumDetail($scope.albumId).then(function (result) {
                if(result.data.adminAlbumVO.programSort==62){
                    $scope.sortableOptions = {
                        axis: 'y',
                        // called after a node is dropped
                        stop: function (e, ui) {
                            if(!programsIsChange()){
                                return false;
                            }
                            var tmpToSave = [];
                            var num =$scope.paginationConf.totalItems+1-($scope.search.pageNo - 1) * $scope.search.pageSize;
                            $scope.programList.forEach(function (oldProgram) {
                                num--;
                                var sort={programId:oldProgram.programId,sort:num};
                                tmpToSave.push(sort);
                            });
                           AlbumManager.albumProgramDragSort($scope.albumId, tmpToSave).then(function (result) {
                                if (result.data.c == "s") {
                                    $state.reload();
                                    ImsAlert.open({title: "排序提示", message: "修改序号成功"}).then(function (result) {

                                    })
                                }
                            })
                        }
                    };

                }else {
                    $scope.sortableOptions = {
                        axis: 'y',
                        // called after a node is dropped
                        stop: function (e, ui) {
                            if(!programsIsChange()){
                                return false;
                            }
                            var tmpToSave = [];
                            var num = ($scope.search.pageNo - 1) * $scope.search.pageSize;
                            $scope.programList.forEach(function (oldProgram) {
                                num++;
                                var sort={programId:oldProgram.programId,sort:num};
                                tmpToSave.push(sort);
                            });
                            AlbumManager.albumProgramDragSort($scope.albumId, tmpToSave).then(function (result) {
                                if (result.data.c == "s") {
                                    $state.reload();
                                    ImsAlert.open({title: "排序提示", message: "修改序号成功"}).then(function (result) {

                                    })
                                }
                            })
                        }
                    };

                }
            })

            $scope.adminList = [{adminId: "", fullName: "审核人"}];
            AlbumManager.albumProgramUsers($scope.albumId).then(function (result) {
                $scope.adminList = $scope.adminList.concat(result.data.pageList);
            })
            ProgramAuditManager.programByAlbumId($scope.albumId, [$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
                if (result.data.c == "n") {
                    $scope.noPermission = true;
                } else {
                    $scope.noPermission = false;
                    $scope.programList = result.data.pageList;
                    $scope.programsCopy =angular.copy($scope.programList);
                    $scope.paginationConf.totalItems = result.data.total;
                }

            })
        } else {
            $scope.adminList = [{adminId: "", fullName: "审核人"}];
            ProgramAuditManager.programCheckUsers().then(function (result) {
                $scope.adminList = $scope.adminList.concat(result.data.pageList);
            })
            ProgramAuditManager.programList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
                if (result.data.c == "n") {
                    $scope.noPermission = true;
                } else {
                    $scope.noPermission = false;
                    $scope.programList = result.data.pageList;
                    $scope.paginationConf.totalItems = result.data.total;
                }

            });
        }


        $scope.sure = function (program) {
            var sort = {targetSort: program.toSort, ownSort: program.sort}
            ProgramAuditManager.programSort(program.albumId, sort).then(function (result) {
                if (result.data.c == "s") {
                    $state.reload();
                }
            })
        }
        $scope.cancel = function (program) {
            program.popOpen = false;
        }

        //调序
        $scope.itemClick = function (id) {

            $scope.programList.forEach(function (item) {
                if (item.programId != id) {
                    item.popOpen = false;
                }
            })
        }

        //初始化全选值为false
        $scope.checkData = {allChecked: false};
        //全选
        $scope.selectAllChange = function (checked) {
            $scope.programList.forEach(function (item) {
                item.checked = checked;
            })
        };
        //选择一个
        $scope.selectOneChange = function (checked) {
            if (!checked) {
                $scope.checkData.allChecked = false;
            } else {
                ($filter('filter')($scope.programList, {checked: true}).length) == $scope.programList.length ? $scope.checkData.allChecked = true : $scope.checkData.allChecked = false;
            }
        };
        $scope.unPass = function () {
            var ids = [];
            $scope.programList.forEach(function (item) {
                if (item.checked) {
                    ids.push(item.programId);
                }
            })

            if (!ids.length) {
                return false;
            }

            ImsDialog.open({title: "节目审核", message: "确定将选中的都设置成不通过吗？"}).then(function (a) {
                if (a == "ok") {
                    ProgramAuditManager.programsUnPass(ids).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        };

        $scope.keyup = function (e, keyword) {

            if (e.key == "Enter") {
                $scope.search.keyword = keyword;
                $scope.search.reviewStatus = $scope.search.reviewStatus == "null" ? null : $scope.search.reviewStatus;
                $state.go($state.current.name, $scope.search);
            }
        }


    }]);

